package com.platform.study.thread;

/**
 * 用实现Runnable接口方式实现线程:线程中断
 * User: wangyanginfo
 * Date: 12-11-26
 * Time: 下午2:44
 */
public class RunnableSimple_4 implements Runnable {

    @Override
    public void run() {
        System.out.println("执行run方法");
        try {
            Thread.sleep(10000); //线程休眠
            for (int i = 0; i < 500; i++ ) {
                System.out.println(Thread.currentThread().getName() + " 执行次数 " + i);
            }
            System.out.println("线程完成休眠");
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.println("休眠被打断");
            return;  //返回到程序的调用处
        }
        System.out.println("线程正常终止");
    }

    public static void main(String[] args) {
        RunnableSimple_4 simpleA = new RunnableSimple_4();
        Thread thread = new Thread(simpleA, "线程1");
        System.out.println("线程启动之前---》" + thread.isAlive());
        thread.start();
        System.out.println("线程启动之后---》" + thread.isAlive());
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        thread.interrupt(); //线程中断
        System.out.println("线程中断之后---》" + thread.isAlive());
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
